package org.example.sensor.service;

import lombok.RequiredArgsConstructor;
import org.example.sensor.dox.SensorData;
import org.example.sensor.repository.SensorDataRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SensorDataService {
    private final SensorDataRepository sensorDataRepository;
    public List<SensorData> findAll() {
        return sensorDataRepository.findAll();
    }
    public List<SensorData> getDataByTimeRange(LocalDateTime start, LocalDateTime end) {
        return sensorDataRepository.findByTimestampBetween(start, end);
    }

    public Page<SensorData> getPagedData(LocalDateTime start, LocalDateTime end, Pageable pageable) {
        long total = sensorDataRepository.countByTimestampBetween(start, end);
        List<SensorData> content = sensorDataRepository.findPagedData(
                start,
                end,
                pageable.getPageSize(),
                pageable.getOffset()
        );
        return new PageImpl<>(content, pageable, total);
    }
}